package audit

import (
	"counter-help/admin/server/global"
	auditReq "counter-help/admin/server/model/audit/request"
	"counter-help/admin/server/model/common/response"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type RequireOrderApi struct{}

// GetRequireOrderList 查看待审核需求
func (r *RequireOrderApi) GetRequireOrderList(c *gin.Context) {
	var pageInfo auditReq.RequestOrderSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := auditRequireService.GetRequireOrderInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
		return
	}

	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// BackOrder 退回订单
func (r *RequireOrderApi) BackOrder(c *gin.Context) {
	var backReq auditReq.BackOrderReq
	if err := c.ShouldBindJSON(&backReq); err != nil {
		global.GVA_LOG.Error("退回订单, 解析参数错误!")
		response.FailWithMessage("退回订单, 解析参数错误!", c)
		return
	}
	err := auditRequireService.BackOrder(&backReq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("退回成功", c)
}

// AccessAudit 通过审核
func (r *RequireOrderApi) AccessAudit(c *gin.Context) {
	var accessReq auditReq.AccessAuditReq
	if err := c.ShouldBindJSON(&accessReq); err != nil {
		global.GVA_LOG.Error("审核订单, 解析参数错误!")
		response.FailWithMessage("审核订单, 解析参数错误!", c)
		return
	}
	err := auditRequireService.AccessAudit(&accessReq)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithMessage("审核成功", c)
}
